<html>
<head><meta charset="utf-8"><title>Box of external type and Drop · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Box.20of.20external.20type.20and.20Drop.html">Box of external type and Drop</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="195988571"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Box%20of%20external%20type%20and%20Drop/near/195988571" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> NeoRaider <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Box.20of.20external.20type.20and.20Drop.html#195988571">(May 01 2020 at 17:48)</a>:</h4>
<p>Oookay, so I have the following setup:</p>
<ul>
<li>no_std</li>
<li>FFI-heavy crate, building safe wrappers around C functions</li>
<li>Custom allocator that is guaranteed just to call C memalign and free (meaning that the alloc Layout is unused for dealloc)</li>
<li>Modeling C pointers to plain old data structs as <code>Box&lt;T&gt;</code> -&gt; With my custom allocator, allocating a struct from C and dropping the Box, or allocating as Box and freeing from C should be fine</li>
<li><code>extern</code> types for opaque C structs<br>
... and now we have a problem: I can Box::from_raw() a pointer to such an external type, but as it's impossible for Rust to determine the Layout, dropping the Box simply does nothing instead of freeing the data</li>
</ul>
<p>Now I'm wondering if it would be possible to extend GlobalAlloc in a way that would allow allocators that don't need to know the layout for dealloc? I assume doing this in a backwards-compatible way would be very difficult and likely not worth the added complexity...</p>
<p>If this is not feasible, should we at least add a lint against creating a Box of an <code>extern</code> type? Directly related: <a href="https://github.com/rust-lang/rust/issues/49708" title="https://github.com/rust-lang/rust/issues/49708">https://github.com/rust-lang/rust/issues/49708</a> - if we had a trait that would disallow size_of_val and align_of_val for <code>extern</code> types, the same trait bound could be added to Box&lt;T&gt;, as a Box of something that can't be deallocated doesn't make sense IMO.</p>
<p>Thoughts?</p>



<a name="196051894"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Box%20of%20external%20type%20and%20Drop/near/196051894" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Box.20of.20external.20type.20and.20Drop.html#196051894">(May 02 2020 at 13:33)</a>:</h4>
<p>My first thought would be to not use <code>Box</code>, but roll your own "owned pointer" type. I think you are leaving the space <code>Box</code> is designed for.</p>



<a name="196052380"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Box%20of%20external%20type%20and%20Drop/near/196052380" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Box.20of.20external.20type.20and.20Drop.html#196052380">(May 02 2020 at 13:46)</a>:</h4>
<p>Implement deref and/or derefmut and drop and you are good to go</p>



<a name="196052958"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Box%20of%20external%20type%20and%20Drop/near/196052958" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Box.20of.20external.20type.20and.20Drop.html#196052958">(May 02 2020 at 13:58)</a>:</h4>
<p>And might as well make your newtype  repr(transparent)</p>



<a name="196055175"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Box%20of%20external%20type%20and%20Drop/near/196055175" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> NeoRaider <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Box.20of.20external.20type.20and.20Drop.html#196055175">(May 02 2020 at 14:54)</a>:</h4>
<p>Thanks, that's what I went for.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>